home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr46 / vfwdk.zip / VFWSDK.ZIP / SAMPLES / BRAVADO / MAPA.ASM < prev    next >
Assembly Source File  |  1993-01-31  |  12KB  |  391 lines

  1.         TITLE MAPA.ASM
  2.         page 60,132
  3.  
  4. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  5. ;
  6. ; MAPA.ASM - Convert from YUV space to 8-bit palettized or 16-bit RGB FAST!!!
  7. ;
  8. ; (C) Copyright Microsoft Corp. 1992-1993.  All rights reserved.
  9. ;
  10. ; You have a royalty-free right to use, modify, reproduce and 
  11. ; distribute the Sample Files (and/or any modified version) in 
  12. ; any way you find useful, provided that you agree that 
  13. ; Microsoft has no warranty obligations or liability for any 
  14. ; Sample Application Files which are modified. 
  15. ;
  16. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  17.  
  18. ?PLM=1        ; PASCAL Calling convention is DEFAULT
  19. ?WIN=0      ; Windows calling convention
  20.  
  21.         .xlist
  22.         include cmacros.inc
  23.         .list
  24.  
  25.         externW         _gwWidthBytes
  26.  
  27. ; -------------------------------------------------------
  28. ;               DATA SEGMENT DECLARATIONS
  29. ; -------------------------------------------------------
  30.  
  31. ifndef SEGNAME
  32.     SEGNAME equ <_TEXT>
  33. endif
  34.  
  35. createSeg %SEGNAME, CodeSeg, word, public, CODE
  36.  
  37. .386
  38.  
  39. sBegin    CodeSeg
  40.         assumes cs,CodeSeg
  41.         assumes ds,nothing
  42.         assumes es,nothing
  43.  
  44. UV65 dw 0, 8h, 10h, 18h, 100h, 108h, 110h, 118h, 200h, 208h, 210h, 218h, 300h, 308h, 310h, 318h
  45. UV43 dw 0, 2h, 04h, 06h, 040h, 042h, 044h, 046h, 080h, 082h, 084h, 086h, 0c0h, 0c2h, 0c4h, 0c6h   
  46. UV21 dw 0, 0h, 01h, 01h,  00h,  00h,  01h,  01h,  20h,  20h,  21h,  21h, 020h, 020h, 021h, 021h   
  47.  
  48. ;┌──────────────────────────────────────────────────────────────────────┐
  49. ;│                                    │
  50. ;│        ┌─────────────────────────────────────────────────┐    │
  51. ;│   Word 0    │ 15 14 13 12 11 10  9  8  7  6  5  4  3  2  1  0 │    │
  52. ;│        │ U6 U5 V6 V5 -- -- -- -- Y6 Y5 Y4 Y3 Y2 Y1 Y0 -- │    │
  53. ;│        └─────────────────────────────────────────────────┘    │
  54. ;│        ┌─────────────────────────────────────────────────┐    │
  55. ;│   Word 1    │ 15 14 13 12 11 10  9  8  7  6  5  4  3  2  1  0 │    │
  56. ;│        │ U4 U3 V4 V3 -- -- -- -- Y6 Y5 Y4 Y3 Y2 Y1 Y0 -- │    │
  57. ;│        └─────────────────────────────────────────────────┘    │
  58. ;│        ┌─────────────────────────────────────────────────┐    │
  59. ;│   Word 2    │ 15 14 13 12 11 10  9  8  7  6  5  4  3  2  1  0 │    │
  60. ;│        │ U2 U1 V2 V1 -- -- -- -- Y6 Y5 Y4 Y3 Y2 Y1 Y0 -- │    │
  61. ;│        └─────────────────────────────────────────────────┘    │
  62. ;│        ┌─────────────────────────────────────────────────┐    │
  63. ;│   Word 3    │ 15 14 13 12 11 10  9  8  7  6  5  4  3  2  1  0 │    │
  64. ;│        │ U0 -- V0 -- -- -- -- -- Y6 Y5 Y4 Y3 Y2 Y1 Y0 -- │    │
  65. ;│        └─────────────────────────────────────────────────┘    │
  66. ;└──────────────────────────────────────────────────────────────────────┘
  67. ; Translates from the above format to palettized 8 bit
  68. ; Simultaneously flips the result vertically into a DIB format
  69.  
  70. cProc   mapUnpackedYUVto8,<NEAR, PASCAL, PUBLIC>,<ds>
  71.         parmD   pDst            ; Destination
  72.         parmD   pSrc            ; Source data
  73.     parmD    pXlat           ; Pointer to YUV555 xlate table
  74.     parmW    wWidth          ; Width in pixels
  75.     parmW    wHeight         ; Height in pixels
  76.     parmW    dxSrc           ; Width of source in bytes
  77.  
  78.     localV    luma,8          ; will contain 5 MSBs of luma
  79.     localW    wTempWidth      ; local copy of width
  80.     localD    dwInc           ; Increment to next line
  81. cBegin
  82.     cld
  83.     push    esi
  84.     push    edi
  85.  
  86.     movzx    edi, di         ; zero the high words
  87.     movzx    esi, si
  88.  
  89.         movzx   eax, dxSrc       ; calc increment to next scanline
  90.         movzx   edx, wWidth
  91.         shl     edx, 1
  92.         add     eax, edx
  93.         mov     dwInc, eax
  94.  
  95.         movzx   edi, dxSrc      ;Bytes per row in frame buffer (or source)
  96.         movzx   eax, wHeight    ;Change Src pointer to bottom of frame
  97.         dec     eax
  98.         mul     edi
  99.     lds    si, pSrc
  100.         add     eax, esi        
  101.         mov     esi, eax
  102.  
  103.     lfs    bx, pXlat
  104.         les     di, pDst
  105.  
  106.         xor     ax, ax          ;zero hi byte of luma array
  107.         mov     luma[0], ax
  108.         mov     luma[2], ax
  109.         mov     luma[4], ax
  110.         mov     luma[6], ax
  111.  
  112. ifdef DEBUG
  113.         or      bx,bx       ; pXlat must be 16:0 !!!
  114.         jz      @f
  115.         int 3
  116.         int 3
  117. @@:
  118. endif
  119.         mov     dx, wWidth
  120.         shr     dx,2         ; 4 pixels processed in the inner loop
  121.         mov     wWidth,dx
  122.  
  123.         mov     ecx, 0f0f8f0f8h  ; chroma and luma mask
  124.  
  125. OuterYUVto8Loop:
  126.         mov     ax, wWidth
  127.         mov     wTempWidth, ax
  128.  
  129.         ALIGN 4
  130.  
  131. mapYUVto8Loop:
  132.         ; get lumas to array, and chromas to dx
  133.         mov     ebx, DWORD PTR ds:[esi]    ;AL = luma 0, AH = chroma 0 (bit 7-4)
  134.         add     esi, 4
  135.         and     ebx, ecx                   ; mask to 5 bits per component
  136.     mov    luma[1],bl
  137.         shr     bx, 11
  138.         mov     dx, cs:[bx+UV65]            ; get U6:5, V6:5 via lookup into dx
  139.  
  140.         ror     ebx, 16
  141.     mov    luma[3],bl
  142.         shr     bx, 11
  143.         or      dx, cs:[bx+UV43]            ; get U4:3, V4:3 via lookup into dx
  144.  
  145.         mov     ebx, DWORD PTR ds:[esi]       ;AL = luma 0, AH = chroma 0 (bit 7-4)
  146.         add     esi, 4
  147.         and     ebx, ecx
  148.     mov    luma[5],bl
  149.         shr     bx, 11
  150.         or      dx, cs:[bx+UV21]            ; get U2, V2 via lookup into dx
  151.  
  152.         ror     ebx, 16
  153.     mov    luma[7],bl
  154.  
  155.         ; combine luma and chroma to 15 bit value
  156.         mov     ebx, luma[0]                
  157.         shr     ebx, 1
  158.         or      bx, dx                  ;merge pix 0
  159.         mov     al,fs:[bx]
  160.  
  161.         ror     ebx, 16
  162.  
  163.         or      bx, dx                  ;merge pix 1
  164.         mov     ah,fs:[bx]
  165.     stos    WORD PTR es:[edi]       ;save 2 palette indices
  166.  
  167.         mov     ebx, luma[4]                
  168.         shr     ebx, 1
  169.         or      bx, dx                  ;merge pix 2
  170.         mov     al,fs:[bx]
  171.  
  172.         ror     ebx, 16
  173.  
  174.         or      bx, dx                  ;merge pix 3
  175.         mov     ah,fs:[bx]
  176.     stos    WORD PTR es:[edi]       ;save 2 palette indices
  177.  
  178.         dec     wTempWidth
  179.         jnz     mapYUVto8Loop
  180.  
  181.         sub     esi,dwInc
  182.  
  183.         dec     wHeight
  184.     jnz    OuterYUVto8Loop
  185.  
  186.     pop    edi
  187.     pop    esi
  188. cEnd
  189.  
  190.  
  191. ;┌──────────────────────────────────────────────────────────────────────┐
  192. ;│                                    │
  193. ;│        ┌─────────────────────────────────────────────────┐    │
  194. ;│   Word 0    │ 15 14 13 12 11 10  9  8  7  6  5  4  3  2  1  0 │    │
  195. ;│        │ U6 U5 V6 V5 -- -- -- -- Y6 Y5 Y4 Y3 Y2 Y1 Y0 -- │    │
  196. ;│        └─────────────────────────────────────────────────┘    │
  197. ;│        ┌─────────────────────────────────────────────────┐    │
  198. ;│   Word 1    │ 15 14 13 12 11 10  9  8  7  6  5  4  3  2  1  0 │    │
  199. ;│        │ U4 U3 V4 V3 -- -- -- -- Y6 Y5 Y4 Y3 Y2 Y1 Y0 -- │    │
  200. ;│        └─────────────────────────────────────────────────┘    │
  201. ;│        ┌─────────────────────────────────────────────────┐    │
  202. ;│   Word 2    │ 15 14 13 12 11 10  9  8  7  6  5  4  3  2  1  0 │    │
  203. ;│        │ U2 U1 V2 V1 -- -- -- -- Y6 Y5 Y4 Y3 Y2 Y1 Y0 -- │    │
  204. ;│        └─────────────────────────────────────────────────┘    │
  205. ;│        ┌─────────────────────────────────────────────────┐    │
  206. ;│   Word 3    │ 15 14 13 12 11 10  9  8  7  6  5  4  3  2  1  0 │    │
  207. ;│        │ U0 -- V0 -- -- -- -- -- Y6 Y5 Y4 Y3 Y2 Y1 Y0 -- │    │
  208. ;│        └─────────────────────────────────────────────────┘    │
  209. ;└──────────────────────────────────────────────────────────────────────┘
  210. ; Translates from the above format to 16 bit RGB DIB format
  211. ; Simultaneously flips the result vertically into a DIB format
  212.  
  213. cProc   mapUnpackedYUVtoRGB16,<NEAR, PASCAL, PUBLIC>,<ds>
  214.         parmD   pDst            ; Destination
  215.         parmD   pSrc            ; Source data
  216.     parmD    pXlat           ; Pointer to YUV555 xlate table
  217.     parmW    wWidth          ; Width in pixels
  218.     parmW    wHeight         ; Height in pixels
  219.     parmW    dxSrc           ; Width of source in bytes
  220.  
  221.     localV    luma,8          ; will contain 5 MSBs of luma
  222.     localW    wTempWidth      ; local copy of width
  223.     localD    dwInc           ; Increment to next line
  224. cBegin
  225.     cld
  226.     push    esi
  227.     push    edi
  228.  
  229.     movzx    edi, di         ; zero the high words
  230.     movzx    esi, si
  231.  
  232.         movzx   eax, dxSrc       ; calc increment to next scanline
  233.         movzx   edx, wWidth
  234.         shl     edx, 1
  235.         add     eax, edx
  236.         mov     dwInc, eax
  237.  
  238.         movzx   edi, dxSrc      ;Bytes per row in frame buffer (or source)
  239.         movzx   eax, wHeight    ;Change Src pointer to bottom of frame
  240.         dec     eax
  241.         mul     edi
  242.     lds    si, pSrc
  243.         add     eax, esi        
  244.         mov     esi, eax
  245.  
  246.     lfs    bx, pXlat
  247.         les     di, pDst
  248.  
  249.         xor     ax, ax          ;zero hi byte of luma array
  250.         mov     luma[0], ax
  251.         mov     luma[2], ax
  252.         mov     luma[4], ax
  253.         mov     luma[6], ax
  254.  
  255. ifdef DEBUG
  256.         or      bx,bx       ; pXlat must be 16:0 !!!
  257.         jz      @f
  258.         int 3
  259.         int 3
  260. @@:
  261. endif
  262.         mov     dx, wWidth
  263.         shr     dx,2         ; 4 pixels processed in the inner loop
  264.         mov     wWidth,dx
  265.  
  266.         mov     ecx, 0f0f8f0f8h  ; chroma and luma mask
  267.  
  268. OuterYUVto16Loop:
  269.         mov     ax, wWidth
  270.         mov     wTempWidth, ax
  271.  
  272.         ALIGN 4
  273.  
  274. mapYUVto16Loop:
  275.         ; get lumas to array, and chromas to dx
  276.         mov     ebx, DWORD PTR ds:[esi]    ;AL = luma 0, AH = chroma 0 (bit 7-4)
  277.         add     esi, 4
  278.         and     ebx, ecx                   ; mask to 5 bits per component
  279.     mov    luma[1],bl
  280.         shr     bx, 11
  281.         mov     dx, cs:[bx+UV65]            ; get U6:5, V6:5 via lookup into dx
  282.  
  283.         ror     ebx, 16
  284.     mov    luma[3],bl
  285.         shr     bx, 11
  286.         or      dx, cs:[bx+UV43]            ; get U4:3, V4:3 via lookup into dx
  287.  
  288.         mov     ebx, DWORD PTR ds:[esi]       ;AL = luma 0, AH = chroma 0 (bit 7-4)
  289.         add     esi, 4
  290.         and     ebx, ecx
  291.     mov    luma[5],bl
  292.         shr     bx, 11
  293.         or      dx, cs:[bx+UV21]            ; get U2, V2 via lookup into dx
  294.  
  295.         ror     ebx, 16
  296.     mov    luma[7],bl
  297.  
  298.         ; combine luma and chroma to 15 bit value
  299.         shl     dx, 1
  300.         mov     ebx, luma[0]                
  301.         or      bx, dx                  ;merge pix 0
  302.         mov     ax,fs:[bx]
  303.     stos    WORD PTR es:[edi]       ;save RGB16
  304.  
  305.         ror     ebx, 16
  306.  
  307.         or      bx, dx                  ;merge pix 1
  308.         mov     ax,fs:[bx]
  309.     stos    WORD PTR es:[edi]       ;save RGB16
  310.  
  311.         mov     ebx, luma[4]                
  312.         or      bx, dx                  ;merge pix 2
  313.         mov     ax,fs:[bx]
  314.     stos    WORD PTR es:[edi]       ;save RGB16
  315.  
  316.         ror     ebx, 16
  317.  
  318.         or      bx, dx                  ;merge pix 3
  319.         mov     ax,fs:[bx]
  320.     stos    WORD PTR es:[edi]       ;save RGB16
  321.  
  322.         dec     wTempWidth
  323.         jnz     mapYUVto16Loop
  324.  
  325.         sub     esi,dwInc
  326.  
  327.         dec     wHeight
  328.     jnz    OuterYUVto16Loop
  329.  
  330.     pop    edi
  331.     pop    esi
  332. cEnd
  333.  
  334.  
  335. ; Fast routine to get the bytes out of the frame buffer
  336. ; so we can set the acquire bit again
  337. cProc   RectCopyBytes,<NEAR, PASCAL, PUBLIC>,<ds>
  338.     parmD    pDst            ; pointer to dest memory
  339.         parmW   wDstWidth       ; width of a dest scanline (in bytes)
  340.         parmD   pSrc            ; pointer to src
  341.     parmW    wSrcWidth       ; width of a src scan (in bytes)
  342.         parmW   xSrc            ; byte offset into scan
  343.         parmW   ySrc            ; scan to start from
  344.     parmW    dxSrc           ; number of bytes (per scan) to copy
  345.     parmW    dySrc           ; number of scans to copy
  346. cBegin
  347.     cld
  348.     push    esi
  349.     push    edi
  350.  
  351.     movzx    edi, di
  352.     movzx    esi, si
  353.         xor     ecx, ecx
  354.  
  355.     lds    si, pSrc
  356.         les     di, pDst
  357.  
  358.         movzx   eax,ySrc
  359.         movzx   edx,wSrcWidth
  360.         mul     edx             ; EAX = ySrc * wSrcWidth
  361.         add     esi,eax         ; pSrc += ySrc * wSrcWidth
  362.         movzx   eax, xSrc
  363.         
  364.         add     esi,eax         ; pSrc += xSrc
  365.  
  366.         mov     dx,dxSrc
  367.  
  368.         mov     bx, wSrcWidth
  369.         sub     bx, dx
  370.  
  371.         mov     ax, wDstWidth
  372.         sub     ax, dx
  373.  
  374.         movzx   eax,ax          ; destination scan line incrementer
  375.         movzx   ebx,bx          ; source scan line incrementer
  376.         shr     dx,1            ; copy word's
  377. @@:     mov     cx,dx
  378.         rep     movs WORD PTR es:[edi], WORD PTR ds:[esi]
  379.         add     esi, ebx
  380.         add     edi, eax
  381.         dec     dySrc
  382.         jnz     @b
  383.  
  384.     pop    edi
  385.     pop    esi
  386. cEnd
  387.  
  388. sEnd    CodeSeg
  389.  
  390. end
  391.